package park.gz.com.parkingfee.until;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import park.gz.com.parkingfee.activity.MyAppcation;

/**
 * Created by Administrator on 2016-11-28.
 */

public class ServiceFile extends Service{
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {

        super.onCreate();

        new Thread() {
            @Override
            public void run() {
                super.run();
                while (true) {

                    try {
                        Thread.sleep(6300000);
                        // 定时查询未配送是否有新的订单并进行语音提示
                        authorize();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
    /**
     *获取code
     */
    public void authorize() {

        String url = ShareUtil.getstringvalue(this,"baseurl") + "/oauth/authorize";
        RequestParams params = new RequestParams(url);
        params.addParameter("client_id", "27d99e12ed99d17fca7602bba157f0cea94d4e49d740f52303944911e723b69e");
        params.addParameter("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
        params.addParameter("response_type", "code");
        Log.i("duzhijuan", params + "");
        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.getString("ref"))) {
                        access_token(jsonObject.getString("code"));
                    } else {
                        Toast.makeText(ServiceFile.this, jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("my", "onError" + ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.e("my", "onCancelled" + cex.toString());
            }

            @Override
            public void onFinished() {
                Log.e("my", "onFinished");
            }

        });
    }

    /**
     * 获取token
     * @param code
     */
    private void access_token(String code) {
        String url = ShareUtil.getstringvalue(this,"baseurl") + "/oauth/token";
        RequestParams params = new RequestParams(url);
        params.addParameter("client_id", "27d99e12ed99d17fca7602bba157f0cea94d4e49d740f52303944911e723b69e");
        params.addParameter("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
        params.addParameter("client_secret", "29f2d01ffc27c6f759626ae3772c1e8776c1744225378811c8f25c0a67f72367");
        params.addParameter("code", code);
        params.addParameter("grant_type", "authorization_code");
        Log.i("duzhijuan", params + "");
        x.http().post(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    ShareUtil.setstringvalue(ServiceFile.this,"access_token",jsonObject.getString("access_token"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("my", "onError" + ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.e("my", "onCancelled" + cex.toString());
            }

            @Override
            public void onFinished() {
                Log.e("my", "onFinished");
            }

        });
    }

}
